package com.zhoug.common3.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.ToastUtils;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * @Author HK-LJJ
 * @Date 2019/11/28
 * @Description
 */
public abstract class AbsFragment extends Fragment {
    protected static final String TAG = ">>>AbsFragment";

    private static final String STATE_SAVE_IS_HIDDEN = "isHidden";
    protected boolean isDestroyView=true;
    private static final boolean DEBUG=false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            boolean isHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            if(DEBUG){
                LogUtils.d(TAG, this.getClass().getSimpleName()+">==>onCreate:isHidden="+isHidden);

            }
            FragmentManager fragmentManager = getFragmentManager();
            if (fragmentManager != null) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                if (isHidden) {
                    fragmentTransaction.hide(this);
                } else {
                    fragmentTransaction.show(this);
                }
                fragmentTransaction.commit();
            }

        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutResId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViews(view);
        isDestroyView=false;
        initView(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isDestroyView=true;
    }



    protected abstract @LayoutRes int getLayoutResId();

    protected abstract void findViews(View view);
    protected abstract void initView(View root);

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
        super.onSaveInstanceState(outState);
        if(DEBUG) {
            LogUtils.d(TAG, this.getClass().getSimpleName() + ">==>onCreate:onSaveInstanceState=" + isHidden());
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(DEBUG) {
            LogUtils.d(TAG, this.getClass().getSimpleName()+">==>onCreate:onHiddenChanged="+hidden);

        }
    }


    //以下是吐司消息
    protected void toastShort(Object msg) {
        if (getActivity() != null) {
            ToastUtils.toastShort(getActivity().getApplicationContext(), msg);
        }
    }

    //以下是吐司消息
    protected void toast(Object msg) {
        toastShort(msg);
    }
    protected void toastLong(Object msg) {
        if (getActivity() != null) {
            ToastUtils.toastLong(getActivity().getApplicationContext(), msg);
        }
    }

    protected void toastShortCenter(Object msg) {
        if (getActivity() != null) {
            ToastUtils.toastShortCenter(getActivity().getApplicationContext(), msg);
        }
    }

    protected void toastLongCenter(Object msg) {
        if (getActivity() != null) {
            ToastUtils.toastLongCenter(getActivity().getApplicationContext(), msg);
        }
    }

}
